package com.heny.demo.strategy;

import com.heny.demo.entity.TurnPoint;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.locationtech.jts.geom.Point;


import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVFileImportStrategy implements FileImportStrategy {
    @Override
    public List<TurnPoint> importData(File file) {
        List<TurnPoint> turnPoints = new ArrayList<>();
        try (CSVParser parser = new CSVParser(new FileReader(file), CSVFormat.DEFAULT.withFirstRecordAsHeader())) {
            for (CSVRecord record : parser) {
                TurnPoint point = new TurnPoint();
                double x = Double.parseDouble(record.get(1)); // X 坐标
                double y = Double.parseDouble(record.get(2)); // Y 坐标
                double z = Double.parseDouble(record.get(3));
                point.setIndex(record.get(0)); // 索引字段直接使用字符串
                point.setCoordinate(TurnPoint.createPoint(x, y, z));
                turnPoints.add(point);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return turnPoints;
    }
}